
# multiple click rates and rewiring probabilities

library(ggplot2)

I <- 36
J <- 4
K <- I*J*6
N = 100
L = 100
T = 8
R = 10
P = 5

prob = matrix(0,P,1)
prob[1,] = .09
prob[2,] = .14
prob[3,] = .19
prob[4,] = .24
prob[5,] = .29

mD = matrix(0,I*J,T)
MD = matrix(0,I*J,T)

SmD = matrix(0,N*L,T)
SMD = matrix(0,N*L,T)

data = matrix(0,0,5)

rand = matrix(0,R,1)
rand[1,1] = 0
rand[2,1] = 10^-2
rand[3,1] = 10^-1.75
rand[4,1] = 10^-1.5
rand[5,1] = 10^-1.25
rand[6,1] = 10^-1
rand[7,1] = 10^-.75
rand[8,1] = 10^-.5
rand[9,1] = 10^-.25
rand[10,1] = 1

# GENERATE NODES

lat <- matrix(1,I,J) 

for(i in 1:I)
{
	for(j in 1:J)
	{
		lat[i,j] = (i-1)*J+j
	}
}

# GENERATE EDGES

conn <- matrix(1,K,2)
count <- 1

for(i in 1:I)
{
	for(j in 1:J)
	{	
		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[i,(j - 2) %% J + 1]
		count <- count + 1

		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[i,j %% J + 1]
		count <- count + 1

		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[(i - 2) %% I + 1,j]
		count <- count + 1

		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[i %% I + 1,j]
		count <- count + 1

		if (j %% 2 == 0)
		{
			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[i %% I + 1,(j - 2) %% J + 1]
			count <- count + 1

			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[i %% I + 1, j %% J + 1]
			count <- count + 1
		}	

		if (j %% 2 == 1)
		{
			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[(i - 2) %% I + 1,(j - 2) %% J + 1]
			count <- count + 1

			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[(i - 2) %% I + 1,j %% J + 1]
			count <- count + 1
		}	
	}
}

connC <- conn

for(r in 1:R)
{
	for(l in 1:L)
	{
		# GENERATE EDGES - Diverse

		count <- 1
		check <- 0

		while(count <= K/2*rand[r,1])
		{
			pick <- c(ceiling(runif(1)*K),ceiling(runif(1)*K))
	
			if(conn[pick[1],1] == conn[pick[2],2] | conn[pick[1],2] == conn[pick[2],1])
			{
				check <- 1
			}

			for(k in 1:K)
			{		
				if(conn[k,1] == conn[pick[1],1] & conn[k,2] == conn[pick[2],2])
				{
					check <- 1
				}

				if(conn[k,1] == conn[pick[2],1] & conn[k,2] == conn[pick[1],2])
				{
					check <- 1
				}
			}

			if (check == 0)
			{
				temp1 <- conn[pick[1],]
				temp2 <- conn[pick[2],]

				for(k in 1:K)
				{	
					if(conn[k,1] == temp1[1] & conn[k,2] == temp1[2])
					{
						conn[k,2] <- temp2[2]
					}

					if(conn[k,1] == temp2[1] & conn[k,2] == temp2[2])
					{
						conn[k,2] <- temp1[2]
					}

					if(conn[k,1] == temp1[2] & conn[k,2] == temp1[1])
					{
						conn[k,2] <- temp2[1]
					}

					if(conn[k,1] == temp2[2] & conn[k,2] == temp2[1])
					{
						conn[k,2] <- temp1[1]
					}
				}

				count <- count + 1
			}

			check <- 0
		}

		connD <- conn
		conn <- connC

		# GENERATE DIFFUSION PROCESSES

		for(p in 1:P)
		{
			for(n in 1:N)
			{
				mD[c(1,1,1,1,1,1),1] = 1
				MD[c(1,1,1,1,1,73),1] = 1
			

				for(t in 2:T)
				{
					a <- connD[which(connD[,1] %in% which(mD[,t-1] %in% 1)),2]
					mD[a,t] = (runif(length(a)) < prob[p,1])

					a <- connD[which(connD[,1] %in% which(MD[,t-1] %in% 1)),2]
					MD[a,t] = (runif(length(a)) < prob[p,1]) 
				}

				for(t in 2:T)
				{
					mD[,t] = apply(mD[,1:t],1,max)
					MD[,t] = apply(MD[,1:t],1,max)
				}

				DmD = cbind(table(9-apply(mD,1,sum)),1,(n-1)*N+l,p,r)
				DMD = cbind(table(9-apply(MD,1,sum)),0,(n-1)*N+l,p,r)

				data = rbind(data,DmD,DMD)
			}

			mD = matrix(0,I*J,T)
			MD = matrix(0,I*J,T)
		}
	}
}

write.table(data, file = "sim2.csv", sep = ",", col.names = FALSE)

q("no")
